#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <memory>

// -------------------- 适配器模式实现 --------------------

// 交易数据结构
struct Transaction {
    std::string date;
    double amount;
    std::string currency;
};

// 数据范围结构
struct DateRange {
    std::string start;
    std::string end;
};

// 财务数据接口
class FinancialDataInterface {
public:
    virtual std::vector<Transaction> fetchTransactions(const DateRange& range) = 0;
    virtual ~FinancialDataInterface() {}
};

// -------------------- 遗留系统适配器 --------------------

// 假设的遗留系统 API
class LegacySystemAPI {
public:
    std::string getDataXML(const std::string& startDate, const std::string& endDate) {
        // 返回一些XML数据
        return "<transactions><transaction><date>2024-11-11</date><amount>100.0</amount><currency>USD</currency></transaction></transactions>";
    }
};

// 遗留系统适配器
class LegacySystemAdapter : public FinancialDataInterface {
private:
    LegacySystemAPI* legacyAPI;

public:
    LegacySystemAdapter() : legacyAPI(new LegacySystemAPI()) {}
    ~LegacySystemAdapter() { delete legacyAPI; }

    std::vector<Transaction> fetchTransactions(const DateRange& range) override {
        std::string xmlData = legacyAPI->getDataXML(range.start, range.end);
        // 解析XML数据，转换为Transaction结构
        std::vector<Transaction> transactions;
        // 这里只是示意，实际应用需要XML解析器
        transactions.push_back({"2024-11-11", 100.0, "USD"});
        return transactions;
    }
};

// -------------------- 现代系统适配器 --------------------

// 假设的现代系统 API
class ModernSystemAPI {
public:
    std::string getDataJSON(const std::string& startDate, const std::string& endDate) {
        // 返回一些JSON数据
        return "{\"transactions\":[{\"date\":\"2024-11-11\",\"amount\":200.0, \"currency\":\"EUR\"}]}";
    }
};

// 现代系统适配器
class ModernSystemAdapter : public FinancialDataInterface {
private:
    ModernSystemAPI* modernAPI;

public:
    ModernSystemAdapter() : modernAPI(new ModernSystemAPI()) {}
    ~ModernSystemAdapter() { delete modernAPI; }

    std::vector<Transaction> fetchTransactions(const DateRange& range) override {
        std::string jsonData = modernAPI->getDataJSON(range.start, range.end);
        // 解析JSON数据，转换为Transaction结构
        std::vector<Transaction> transactions;
        // 这里只是示意，实际应用需要JSON解析器
        transactions.push_back({"2024-11-11", 200.0, "EUR"});
        return transactions;
    }
};

// -------------------- 桥接模式实现 --------------------

// 抽象类 DataFetcher 作为桥接
class DataFetcher {
protected:
    FinancialDataInterface* impl;  // 指向实现部分的指针

public:
    DataFetcher(FinancialDataInterface* implementation) : impl(implementation) {}
    virtual ~DataFetcher() {}
    
    // 获取数据的方法
    virtual std::vector<Transaction> fetchData(const DateRange& range) {
        return impl->fetchTransactions(range);
    }
};

// 继承自 DataFetcher的具体类，添加额外的调试输出
class VerboseDataFetcher : public DataFetcher {
public:
    VerboseDataFetcher(FinancialDataInterface* implementation) : DataFetcher(implementation) {}

    std::vector<Transaction> fetchData(const DateRange& range) override {
        std::cout << "VerboseDataFetcher: Fetching data from " << range.start << " to " << range.end << std::endl;
        std::vector<Transaction> data = DataFetcher::fetchData(range);
        std::cout << "VerboseDataFetcher: Data fetching complete. Number of transactions: " << data.size() << std::endl;
        return data;
    }
};

// -------------------- 测试用例 --------------------

int main() {
    std::cout << "===== 适配器模式测试 =====\n";

    // 创建适配器实例
    std::unique_ptr<FinancialDataInterface> legacyAdapter = std::make_unique<LegacySystemAdapter>();
    std::unique_ptr<FinancialDataInterface> modernAdapter = std::make_unique<ModernSystemAdapter>();

    // 定义一个日期范围
    DateRange range = {"2024-11-01", "2024-11-30"};

    // 使用适配器获取交易数据
    std::cout << "\n--- Fetching transactions using LegacySystemAdapter ---\n";
    std::vector<Transaction> legacyTransactions = legacyAdapter->fetchTransactions(range);
    for (const auto& txn : legacyTransactions) {
        std::cout << "Transaction from LegacySystemAdapter: " << txn.date << ", " << txn.amount << " " << txn.currency << "\n";
    }

    std::cout << "\n--- Fetching transactions using ModernSystemAdapter ---\n";
    std::vector<Transaction> modernTransactions = modernAdapter->fetchTransactions(range);
    for (const auto& txn : modernTransactions) {
        std::cout << "Transaction from ModernSystemAdapter: " << txn.date << ", " << txn.amount << " " << txn.currency << "\n";
    }

    std::cout << "\n===== 桥接模式测试 =====\n";

    // 创建桥接实例
    VerboseDataFetcher legacyFetcher(new LegacySystemAdapter());
    VerboseDataFetcher modernFetcher(new ModernSystemAdapter());

    // 获取数据
    std::cout << "\n--- Fetching transactions using VerboseDataFetcher with LegacySystemAdapter ---\n";
    std::vector<Transaction> fetchedLegacyTransactions = legacyFetcher.fetchData(range);
    for (const auto& txn : fetchedLegacyTransactions) {
        std::cout << "Fetched Transaction from LegacySystemAdapter: " << txn.date << ", " << txn.amount << " " << txn.currency << "\n";
    }

    std::cout << "\n--- Fetching transactions using VerboseDataFetcher with ModernSystemAdapter ---\n";
    std::vector<Transaction> fetchedModernTransactions = modernFetcher.fetchData(range);
    for (const auto& txn : fetchedModernTransactions) {
        std::cout << "Fetched Transaction from ModernSystemAdapter: " << txn.date << ", " << txn.amount << " " << txn.currency << "\n";
    }

    return 0;
}
